package ws.jdbc.concurrent.statement;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.concurrent.locks.ReentrantLock;

/**
 * This class implements {@link IStatement} providing some methods to concurrently
 * add and execute a SQL batch.
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class EStatement implements IStatement {

  private Statement stmt;
  private final ReentrantLock lock = new ReentrantLock();

  public EStatement(Statement stmt) {
    this.stmt = stmt;
  }

  public void addBatch(String sql) throws SQLException {
    lock.lock();
    try {
      stmt.addBatch(sql);
    } finally {
      lock.unlock();
    }
  }

  public void clearBatch() throws SQLException {
    lock.lock();
    try {
      stmt.clearBatch();
    } finally {
      lock.unlock();
    }
  }

  public int[] executeBatch() throws SQLException {
    lock.lock();
    try {
      return stmt.executeBatch();
    } finally {
      lock.unlock();
    }
  }

  public final void lock() {
    lock.lock();
  }

  public synchronized final void unlock() {
    for (int i = 0; i < lock.getHoldCount(); ++i) {
      lock.unlock();
    }
  }

  // <editor-fold defaultstate="collapsed" desc="java.sql.Statement Methods">
  public ResultSet executeQuery(String sql) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public int executeUpdate(String sql) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public void close() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getMaxFieldSize() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setMaxFieldSize(int max) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getMaxRows() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setMaxRows(int max) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setEscapeProcessing(boolean enable) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getQueryTimeout() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setQueryTimeout(int seconds) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public void cancel() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public SQLWarning getWarnings() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public void clearWarnings() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setCursorName(String name) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean execute(String sql) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public ResultSet getResultSet() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getUpdateCount() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean getMoreResults() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setFetchDirection(int direction) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getFetchDirection() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setFetchSize(int rows) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getFetchSize() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getResultSetConcurrency() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getResultSetType() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public Connection getConnection() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean getMoreResults(int current) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public ResultSet getGeneratedKeys() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public int executeUpdate(String sql, String[] columnNames) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean execute(String sql, int[] columnIndexes) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean execute(String sql, String[] columnNames) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public int getResultSetHoldability() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean isClosed() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public void setPoolable(boolean poolable) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean isPoolable() throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public <T> T unwrap(Class<T> iface) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  public boolean isWrapperFor(Class<?> iface) throws SQLException {
    throw new UnsupportedOperationException("Not supported yet.");
  }
  // </editor-fold>
}
